<?php

namespace App\Http\Controllers\SubAccount;

use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\SubAccount\SubAccount;
use App\Models\SubAccount\SubAccountAssignStaffLog;
use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use App\Models\SubAccount\SubAccountRepayLog;
use App\Models\SubAccount\SubAccountStaffBind;
use App\Models\SystemUser;
use Illuminate\Http\Request;

class SubAccountManageController extends Controller
{

    //
    public function departments(Request $request)
    {
        $subAccount=SubAccount::find($request->sub_account_id);
        $departId=$subAccount->shop->department_id;
        $departments=Department::where('id',$departId)->orWhere('pid','like','%'.$departId.'%')->get();
        return $this->success($departments);
    }

    //
    public function departmentStaff(Request $request)
    {
        $departID=$request->department_id;
        $users=SystemUser::where('department_id',$departID)->get();
        return $this->success($users);
    }

    //分配客服
    public function assignStaff(Request $request)
    {
        if(!$request->filled('staff_ids')){
            return $this->failed('客服id不能为空',201);
        }
        if($request->staff_ids){
            $staffIds=explode(',',$request->staff_ids);
            if(empty($staffIds)){
                return $this->failed('客服id不能为空',201);
            }
        }
        $accountId=$request->sub_account_id;
        $subAccount=SubAccount::find($request->sub_account_id);
        if(empty($subAccount)){
            return $this->failed('子账号id错误',201);
        }
        $delRes=$this->delStaffBinds($accountId,$staffIds,$request->is_backup);
        foreach ($staffIds as $id){
            $where=[
                'sub_account_id'=>$accountId,
                'staff_id'=>$id,
            ];
            $data=$where;
            $data['assign_admin_id']=auth()->user()->id??'';
            $data['is_backup']=$request->is_backup;
            SubAccountStaffBind::updateOrCreate($where,$data);

            //分配记录
            SubAccountAssignStaffLog::create($data);
        }
        $this->updateSubAccount($subAccount,$request);
        return $this->success(['msg'=>'分配成功']);
    }
    //删除不再负责此账号的
    public function delStaffBinds($accountId,$staffIds,$isBackup){
        return SubAccountStaffBind::where('sub_account_id',$accountId)
            ->where('is_backup',$isBackup)
            ->whereNotIn('staff_id',$staffIds)
            ->delete();
    }
    //更新子账号相关信息
    public function updateSubAccount($subAccount,$request){
        if($request->filled('department3_id')){
            $subAccount->department3_id=$request->department3_id;
            $subAccount->department_id=$request->department3_id;
            $shopData=[
                'department_three_id'=>$request->department3_id,
                'department_id'=>$request->department3_id,
            ];
            $subAccount->shop()->update($shopData);
        }
        $subAccount->assign_staff_time=now();
        $subAccount->assign_staff_times+=1;
        if($subAccount->assign_staff_times==3){
            $subAccount->multi_change_status=1;
        }
        $subAccount->save();
        return $this->updateLoginOutLog($subAccount);
    }
    //
    public function updateLoginOutLog($subAccount){
        $data=[
            'department3_id'=>$subAccount->department3_id,
            'department3'=>$subAccount->department3->name,
            'department_id'=>$subAccount->department3_id,
        ];
        $where=[
            'sub_account_id'=>$subAccount->id,
            'department3_id'=>null,
        ];
        return SubAccountLoginLogoutRecord::where($where)->update($data);
    }
    //删除不再负责此账号的
    public function delOffStaff($subAccount,$staffIds){
        SubAccountStaffBind::where('sub_account_id',$subAccount->id)->whereNotIn('staff_id',$staffIds)->delete();
    }

    //分配记录
    public function assignLogs(Request $request){
        $logs=SubAccountAssignStaffLog::with($this->assignLogWith())->where('sub_account_id',$request->sub_account_id??0)->get();
        return $this->success($logs);
    }
    //
    public function assignLogWith(){
        return [
            'subAccount:id,sub_account,shop_id',
            'assignAdmin:id,name,department_one_id,department_two_id,department_three_id',
            'staff:id,name,department_one_id,department_two_id,department_three_id',
        ];
    }

    //续费
    public function repay(Request $request)
    {
        $subAccount=SubAccount::find($request->sub_account_id);
        $before=$subAccount->service_time_end;
        $after=$request->service_time_end;
        $subAccount->service_time_end=$request->service_time_end;
        $subAccount->save();
        $this->repayLog($subAccount,$before,$after,$request);
        return $this->success(['msg'=>'续费成功']);
    }
    //续费记录
    public function repayLog($subAccount,$before,$after,$request){
        $data=[
            'sub_account_id'=>$subAccount->id,
            'before_service_time_end'=>$before,
            'after_service_time_end'=>$after,
            'repay_admin_id'=>auth()->user()->id??'',
            'repay_amount'=>$request->repay_amount??0,
        ];
        SubAccountRepayLog::create($data);
    }
    //列表
    public function repayLogs(Request $request){
        $data=SubAccountRepayLog::where('sub_account_id',$request->sub_account_id??0)
            ->orderByDesc('id')
            ->paginate($request->size??20);
        return $this->success($data);
    }
    //更新
    public function updateRepayLog(Request $request){
        $repayLog=SubAccountRepayLog::find($request->repay_log_id??0);
        if(empty($repayLog)){
            return $this->failed('id错误，无此续费记录',201);
        }
        $data=$request->except('repay_log_id');
        $repayLog->update($data);
        return $this->success(['msg'=>'更新成功']);
    }

    //停用账号
    public function disable(Request $request)
    {
        $subAccount=SubAccount::find($request->sub_account_id);
        if($subAccount->status==1){
            $subAccount->status=0;
            $subAccount->breakup_time=date('Y-m-d H:i:s');
            $subAccount->save();
        }
        return $this->success(['msg'=>'操作成功']);
    }


}
